package cn.codeforfun.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessInstanceWithVariablesImpl;
import org.camunda.bpm.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangbin
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProcessInstanceVO {
    private String processInstanceId;
    private String processDefinitionId;
    private String rootProcessInstanceId;
    private String businessKey;

    private Map<String, Object> variables;

    public static ProcessInstanceVO from(HistoricProcessInstance source) {
        ProcessInstanceVO target = new ProcessInstanceVO();
        target.setProcessInstanceId(source.getId());
        target.setBusinessKey(source.getBusinessKey());
        target.setProcessDefinitionId(source.getProcessDefinitionId());
        target.setRootProcessInstanceId(source.getRootProcessInstanceId());
        Map<String, Object> variables = new HashMap<>(0);
        if (source instanceof ProcessInstanceWithVariablesImpl) {
            try {
                variables = ((ProcessInstanceWithVariablesImpl) source).getVariables();
            } catch (NullPointerException e) {
                try {
                    variables = ((ProcessInstanceWithVariablesImpl) source).getVariables();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        } else if (source instanceof ExecutionEntity) {
            try {
                variables = ((ExecutionEntity) source).getVariables();
            } catch (NullPointerException e) {
                try {
                    variables = ((ExecutionEntity) source).getVariables();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }

        target.setVariables(variables);
        return target;
    }

    public static ProcessInstanceVO from(ProcessInstance source) {
        ProcessInstanceVO target = new ProcessInstanceVO();
        target.setProcessInstanceId(source.getProcessInstanceId());
        target.setBusinessKey(source.getBusinessKey());
        target.setProcessDefinitionId(source.getProcessDefinitionId());
        target.setRootProcessInstanceId(source.getRootProcessInstanceId());
        Map<String, Object> variables = new HashMap<>(0);
        if (source instanceof ProcessInstanceWithVariablesImpl) {
            try {
                variables = ((ProcessInstanceWithVariablesImpl) source).getVariables();
            } catch (NullPointerException e) {
                try {
                    variables = ((ProcessInstanceWithVariablesImpl) source).getVariables();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        } else if (source instanceof ExecutionEntity) {
            try {
                variables = ((ExecutionEntity) source).getVariables();
            } catch (NullPointerException e) {
                try {
                    variables = ((ExecutionEntity) source).getVariables();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }

        target.setVariables(variables);
        return target;
    }
}
